package com.redsoil.house.ui.login.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.redsoil.house.R;
import com.redsoil.house.base.BaseActivity;
import com.redsoil.house.base.RxBus;
import com.redsoil.house.model.event.LogoutEvent;
import com.redsoil.house.ui.login.contract.ChangePwdContract;
import com.redsoil.house.ui.login.contract.ForgotPwdContract;
import com.redsoil.house.ui.login.presenter.ChangePwdPresenter;
import com.redsoil.house.ui.login.presenter.FotgotPwdPresenter;
import com.redsoil.house.widget.actionbar.XBaseTopBar;
import com.redsoil.house.widget.actionbar.XBaseTopBarImp;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 修改密码
 */
public class ChangePwdActivity extends BaseActivity<ChangePwdPresenter> implements ChangePwdContract.View,XBaseTopBarImp{

    @BindView(R.id.main_topbar)
    XBaseTopBar main_topbar;

    @BindView(R.id.old_password_edit)
    EditText old_password_edit;

    @BindView(R.id.new_password_edit)
    EditText new_password_edit;

    @BindView(R.id.renew_password_edit)
    EditText renew_password_edit;

    @BindView(R.id.change_sure_button)
    Button change_sure_button;

    @BindView(R.id.input_layout)
    AutoLinearLayout input_layout;

    @BindView(R.id.success_layout)
    AutoRelativeLayout success_layout;

    @Override
    protected int getLayout() {
        return R.layout.activity_change_pwd;
    }

    @Override
    protected void initEventAndData() {
        main_topbar.setxBaseTopBarImp(this);
    }

    @Override
    public void responeError(String errorMsg) {
        ToastUtils.showShortToast("密码修改失败");
    }

    @Override
    public void modifyPwdSuccess() {
//        ToastUtils.showShortToast("密码修改成功");
        input_layout.setVisibility(View.GONE);
        success_layout.setVisibility(View.VISIBLE);

//        RxBus.getDefault().post(new LogoutEvent(LogoutEvent.LOGOUT));
//        finish();
    }

    @OnClick(R.id.change_sure_button)
    void onChangeSureButton(){

        String oldPassword = old_password_edit.getText().toString();
        if (StringUtils.isSpace(oldPassword)){
            ToastUtils.showShortToast("原密码不能为空");
            return;
        }

        String newPassword = renew_password_edit.getText().toString();
        if (StringUtils.isSpace(newPassword)){
            ToastUtils.showShortToast("新密码不能为空");
            return;
        }

        mPresenter.modifyPwd(oldPassword,newPassword);
    }

    @Override
    public void left() {
        if(success_layout.getVisibility() == View.VISIBLE){
            RxBus.getDefault().post(new LogoutEvent(LogoutEvent.LOGOUT));
        }
        finish();
    }

    @Override
    public void right() {}

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    /**
     * 打开新Activity
     *
     * @param context
     * @return
     */
    public static Intent newInstance(Context context) {
        Intent intent = new Intent(context, ChangePwdActivity.class);
        return intent;
    }

}
